package filesystem.adapters;

import javax.swing.Icon;
import javax.swing.JLabel;

import org.eclipse.emf.ecore.EObject;

import filesystem.File;
import filesystem.FileType;
import filesystem.FilesystemPackage;
import filesystem.Utils;
import filesystem.util.FilesystemSwitch;

/**
 * Adapter that will adapt a {@link FileType} to {@link JLabel} icon. It will
 * track changes in {@link File#getType()} and update icon accordingly.
 * 
 * @author Renat Zubairov, Igor Drobiazko
 */
public class IconAdapter extends AbstractAdapter {

	JLabel label;

	/**
	 * Public constructor with label where icon should be put
	 * 
	 * @param label
	 */
	public IconAdapter(JLabel label) {
		super(FilesystemPackage.FILE_TYPE);
		this.label = label;
	}

	/**
	 * This method is called by superclass when update is needed (when file type
	 * was changed and/or target is set)
	 */
	@Override
	protected void update(EObject object) {
		String name = icons.doSwitch(object);
		Icon icon = Utils.getIcon(name);
		label.setIcon(icon);
	}

	/**
	 * This private class is used for transform {@link FileType} to
	 * {@link String} with icon name
	 */
	FilesystemSwitch<String> icons = new FilesystemSwitch<String>() {
		
		/**
		 * This method will be called when we have
		 * {@link File} as parameter to switch
		 */
		@Override
		public String caseFile(File object) {
			int value = object.getType().getValue();
			switch (value) {
			case FileType.TEXT_VALUE:
				return "text.png"; 
			case FileType.IMAGE_VALUE:
				return "image.png";
			case FileType.AUDIO_VALUE:
				return "audio.png";
			default:
				break;
			}
			return null;
		}
		
		/**
		 * This method will be called when no other
		 * caseXXX method was called before or
		 * caseXXX method returned null
		 */
		@Override
		public String defaultCase(EObject object) {
			return "error.png";
		}

	};
}
